package com.springboot.controller;

import com.springboot.dto.OrderDoctorList;
import com.springboot.model.*;
import com.springboot.service.PatientMyOrderService;
import com.springboot.service.PatientOrderViewService;
import com.springboot.service.UserLoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.ArrayList;

/**
 * 用于患者点击就诊纪录时，进行的响应
 */
@Controller
public class PatientMedicalRecordController {
    @Autowired
    PatientMyOrderService patientMyOrderService;

    @Autowired
    UserLoginService userLoginService;

    /**
     * 患者点击就诊记录时
     * @param phone
     * @return orderDoctorLists
     */
    @CrossOrigin
    @RequestMapping(value = "/patientmedicalrecord")
    public @ResponseBody
    ArrayList<OrderDoctorList> patientMyOrder(@RequestParam("unitphone") String phone,
                                              @RequestParam("flag") Integer flag,
                                              @RequestParam("searchText") String content) {
        UserLoginForm userLoginForm = userLoginService.findUserByTel(phone);
        Integer userId = userLoginForm.getUserId();
        ArrayList<OrderDoctorList> orderDoctorLists = patientMyOrderService.showOrderDoctors(userId);
        System.out.println(userId+" 正在查看就诊记录");
        //模糊搜索
        Integer A = 1;
        ArrayList<OrderDoctorList> orderDoctorLists1 = new ArrayList<>();
        if(flag.equals(A))
        {
            OrderDoctorList orderDoctorList = new OrderDoctorList();
            for(int i=0;i<orderDoctorLists.size();i++)
            {
                orderDoctorList = orderDoctorLists.get(i);
                String doctorName = orderDoctorList.getDoctorName();
                String doctorRank = orderDoctorList.getDoctorRank();
//                ArrayList<DoctorFieldForm> doctorFieldForms = orderDoctorList.getDoctorField();
//                DoctorFieldForm doctorFieldForm = doctorFieldForms.get(0);
                //String doctorField = doctorFieldForm.getDoctorField();
                OrderForm orderForm = orderDoctorList.getOrderForm();
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
                String bootDate = simpleDateFormat.format(orderForm.getBootDate());
                String bootTime = orderForm.getBootTime();
                String progress = orderForm.getProgress();
                int tag = 0;
                if(doctorName.contains(content)&&tag==0)
                {
                    tag=1;
                    orderDoctorLists1.add(orderDoctorList);
                }
                if(doctorRank.contains(content)&&tag==0)
                {
                    tag=1;
                    orderDoctorLists1.add(orderDoctorList);
                }
//                if(doctorField.contains(content)&&tag==0)
//                {
//                    tag=1;
//                    orderDoctorLists1.add(orderDoctorList);
//                }
                if(bootDate.contains(content)&&tag==0)
                {
                    tag=1;
                    orderDoctorLists1.add(orderDoctorList);
                }
                if(bootTime.contains(content)&&tag==0)
                {
                    tag=1;
                    orderDoctorLists1.add(orderDoctorList);
                }
                if(progress.contains(content)&&tag==0)
                {
                    tag=1;
                    orderDoctorLists1.add(orderDoctorList);
                }
            }
            orderDoctorLists = orderDoctorLists1;
        }
       return orderDoctorLists;
    }

    /**
     * 用户在就诊记录点击某次记录时
     * @param bookId
     * @return PatientInfoForm

     * @date 2020-12-20
     */
    @CrossOrigin
    @RequestMapping(value = "/patientmedicalrecord/seeoneinfo")
    public @ResponseBody
    PatientInfoForm patientMedicalRecordSeeOneInfo(@RequestParam("bookId") Integer bookId) {
        return patientMyOrderService.showMedicalInfoByBookId(bookId);
    }
}
